package com.biz.primus.management.promotion.controller;

import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.base.vo.PageVo;
import com.biz.primus.base.vo.promotion.PromotionProductReqVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.promotion.feign.PromotionFeignClient;
import com.biz.primus.model.promotion.vo.CutPriceSalePromotionVo;
import com.biz.primus.model.promotion.vo.CutPriceStairVo;
import com.google.common.collect.Lists;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 降价拍
 *
 * @author david-liu
 */
@Controller
@RequestMapping("management/cutPriceSalePromotion")
public class CutPriceSalePromotionController extends AbstractBaseController {

    @Autowired
    private PromotionFeignClient promotionFeignClient;

    @RequestMapping(value = "list")
    public ModelAndView list(PageVo pageVo) {
        SpringPageVO<CutPriceSalePromotionVo> pageVO = promotionFeignClient.getCutPriceSalePromotions(pageVo);
        return new ModelAndView("cut_price_sale/list").addObject("page", pageVO);
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit(Long id) {
        CutPriceSalePromotionVo cutPriceSalePromotionVo = promotionFeignClient.getCutPriceSalePromotion(id);
        return new ModelAndView("cut_price_sale/add").addObject("cutPriceSalePromotion", cutPriceSalePromotionVo);
    }

    @RequestMapping(value = "statusToggle")
    public ModelAndView statusToggle(Long id) {
        if (!promotionFeignClient.toggleCutPriceSalePromotionStatus(id)) {
            throw new BizBusinessException("切换活动状态出错");
        }
        return new ModelAndView("redirect:list.do");
    }

    @ResponseBody
    @RequestMapping(value = "rules")
    public String promotionRules(Long id) {
        List<CutPriceStairVo> ret = Lists.newArrayList();
        IntStream.rangeClosed(1, 5).forEach(index -> {
            CutPriceStairVo vo = new CutPriceStairVo();
            vo.setStartTime(LocalDateTime.now());
            vo.setEndTime(LocalDateTime.now());
            vo.setSalePrice(index);
            ret.add(vo);
        });
        return JsonUtils.obj2Json(ret);
    }

    @RequestMapping(value = "new")
    public ModelAndView create() {
        return new ModelAndView("cut_price_sale/add");
    }

    @RequestMapping(value = "/products")
    @ResponseBody
    public String promotionProducts(Long promotionId) {
        return "";
    }

    @RequestMapping(value = "/products/search")
    @ResponseBody
    public String searchPromotionProducts(PromotionProductReqVo reqVo) {
        return "";
    }
}
